home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / split.c < prev    next >
C/C++ Source or Header  |  1990-07-15  |  2KB  |  107 lines

  1. /* split - split a file            Author: Michiel Huisjes */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <blocksize.h>
  6.  
  7. int cut_line = 1000;
  8. int infile;
  9. char out_file[100];
  10. char *suffix;
  11.  
  12. main(argc, argv)
  13. int argc;
  14. char **argv;
  15. {
  16.   unsigned short i;
  17.  
  18.   out_file[0] = 'x';
  19.   infile = -1;
  20.  
  21.   if (argc > 4) usage();
  22.   for (i = 1; i < argc; i++) {
  23.     if (argv[i][0] == '-') {
  24.         if (argv[i][1] >= '0' && argv[i][1] <= '9'
  25.             && cut_line == 1000)
  26.             cut_line = atoi(argv[i]);
  27.         else if (argv[i][1] == '\0' && infile == -1)
  28.             infile = 0;
  29.         else
  30.             usage();
  31.     } else if (infile == -1) {
  32.         if ((infile = open(argv[i], O_RDONLY)) < 0) {
  33.             std_err("Cannot open input file.\n");
  34.             exit(1);
  35.         }
  36.     } else
  37.         strcpy(out_file, argv[i]);
  38.   }
  39.   if (infile == -1) infile = 0;
  40.   strcat(out_file, "aa");
  41.   for (suffix = out_file; *suffix; suffix++);
  42.   suffix--;
  43.  
  44. /* Appendix now points to last `a' of "aa". We have to decrement it by one */
  45.   *suffix = 'a' - 1;
  46.   split();
  47.   exit(0);
  48. }
  49.  
  50. split()
  51. {
  52.   char buf[BLOCK_SIZE];
  53.   register char *index, *base;
  54.   register int n;
  55.   int fd;
  56.   long lines = 0L;
  57.  
  58.   fd = -1;
  59.   while ((n = read(infile, buf, BLOCK_SIZE)) > 0) {
  60.     base = index = buf;
  61.     while (--n >= 0) {
  62.         if (*index++ == '\n')
  63.             if (++lines % cut_line == 0) {
  64.                 if (fd == -1) fd = newfile();
  65.                 if (write(fd, base, (int) (index - base)) != (int) (index - base))
  66.                     quit();
  67.                 base = index;
  68.                 close(fd);
  69.                 fd = -1;
  70.             }
  71.     }
  72.     if (index == base) continue;
  73.     if (fd == -1) fd = newfile();
  74.     if (write(fd, base, (int) (index - base)) != (int) (index - base))
  75.         quit();
  76.   }
  77. }
  78.  
  79. newfile()
  80. {
  81.   int fd;
  82.  
  83.   if (++*suffix > 'z') {    /* Increment letter */
  84.     *suffix = 'a';        /* Reset last letter */
  85.     ++*(suffix - 1);    /* Previous letter must be incremented */
  86.     /* E.g. was `filename.az' */
  87.     /* Now `filename.ba' */
  88.   }
  89.   if ((fd = creat(out_file, 0644)) < 0) {
  90.     std_err("Cannot create new file.\n");
  91.     exit(2);
  92.   }
  93.   return fd;
  94. }
  95.  
  96. usage()
  97. {
  98.   std_err("Usage: split [-n] [file [name]].\n");
  99.   exit(1);
  100. }
  101.  
  102. quit()
  103. {
  104.   std_err("split: write error\n");
  105.   exit(1);
  106. }
  107.